package com.sreader.sreader4j;

import java.util.List;
import java.util.Locale;

import com.sreader.interfaces.Feuille;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Button;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Form;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

public class FeuilleDeCalculCroise extends CustomComponent{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Window mainWindow;
	private Locale currentLocale;
	private String currentLocaleValue;
	private Window calculCroise;
	private Panel mainPanel;
	private AbsoluteLayout mainPanelAbsoluteLayout;
	private FeuilleDeIdentite feuilleIdentiteUn;
	private FeuilleDeIdentite feuilleIdentiteDeux;
	private FeuilleDeResultatCroise feuilleDeResultatCroise;
	private AbsoluteLayout mainLayout;
	private PersonBean firstPerson;
	private PersonBean secondPerson;
	private Messages messages;
	public Window mainWindowCroise;
	private Button sendButton;
	private ClickListener sendButtonListener;
	private Form feuilleCroiseForm;
	private AbsoluteLayout formAbsoluteLayout;
	private ApplicationMenuBar menuBar;
	
	public FeuilleDeCalculCroise(Locale defaultLocale, Window mainWindow){
		this.mainWindow = mainWindow;
		setCurrentLocale(defaultLocale);
		setMessages(new Messages("FeuilleDeCalculCroise",defaultLocale));
		mainWindowCroise = new Window("Sreader4u");
		firstPerson = new PersonBean();
		secondPerson= new PersonBean();
		feuilleDeResultatCroise = new FeuilleDeResultatCroise(defaultLocale, mainWindow); 
		//mainWindowCroise.setName("analysecroise");
		setCurrentLocaleValue(defaultLocale.toString());
		buildMainAbsolutLayout();
		setCompositionRoot(mainLayout);
		
	}
	
	
	
	
	public Locale getCurrentLocale() {
		return currentLocale;
	}




	public void setCurrentLocale(Locale currentLocale) {
		this.currentLocale = currentLocale;
	}


	private AbsoluteLayout buildMainAbsolutLayout(){
		mainLayout = new AbsoluteLayout();
		mainLayout.setCaption(getMessages().getString("feuilleDescriptionA"));
		mainLayout.setImmediate(false);
		mainLayout.setWidth("725.0px"); 
		mainLayout.setHeight("920.0px"); 
		mainLayout.setMargin(false);
		
		setWidth("750.0px"); 
		setHeight("950.0px");
		//menuBar= new ApplicationMenuBar(getCurrentLocale(), mainWindow);
		//mainLayout.addComponent(menuBar.getApplicationMenuBar(), "top:0.0px;left:10.0px;");
		buildMainPanel();
		mainLayout.addComponent(mainPanel, "top:20.0px;left:10.0px;");
		
		return this.mainLayout;
	}
	
	public Panel buildMainPanel(){
		mainPanel = new Panel(this.messages.getString("feuilleCaption"));
		mainPanel.setImmediate(true);
		mainPanel.setWidth("710.0px"); //720.0
		mainPanel.setHeight("900px"); //900.0
		mainPanel.getContent().setSizeUndefined();
		mainPanelAbsoluteLayout = buildMainPanelAbsoluteLayout();
		
		mainPanel.setContent(mainPanelAbsoluteLayout);
		return mainPanel;
	}
	
	public AbsoluteLayout buildMainPanelAbsoluteLayout(){
		mainPanelAbsoluteLayout = new AbsoluteLayout();
		mainPanelAbsoluteLayout.setImmediate(true);
	
		mainPanelAbsoluteLayout.setWidth("700.0px"); //715.0
		mainPanelAbsoluteLayout.setHeight("890.0px"); //870.0
		mainPanelAbsoluteLayout.setMargin(false);
		
		
		this.feuilleCroiseForm = buildFeuilleCroiseForm();
		
		mainPanelAbsoluteLayout.addComponent(this.feuilleCroiseForm);
		
		return mainPanelAbsoluteLayout;
	}
	
	public Form buildFeuilleCroiseForm(){
		Form feuilleCroiseForm = new Form();
		feuilleCroiseForm.setImmediate(true);
		feuilleCroiseForm.setDescription(this.messages.getString("feuilleDescription"));
		feuilleCroiseForm.setWidth("690.0px");
		feuilleCroiseForm.setHeight("880.0px");
		this.formAbsoluteLayout = buildFormAbsolutLayout();
		feuilleCroiseForm.setLayout(this.formAbsoluteLayout);
		return feuilleCroiseForm;
		
	}
	
	public AbsoluteLayout buildFormAbsolutLayout(){
		AbsoluteLayout formAbsoluteLayout = new AbsoluteLayout();
		formAbsoluteLayout.setImmediate(true);
		formAbsoluteLayout.setWidth("680.0px"); 
		formAbsoluteLayout.setHeight("878.0px");
		formAbsoluteLayout.setMargin(false);
		feuilleIdentiteUn = new FeuilleDeIdentite(currentLocale, this.mainWindow);
		feuilleIdentiteDeux = new FeuilleDeIdentite(currentLocale, this.mainWindow);
		formAbsoluteLayout.addComponent(feuilleIdentiteUn);
		formAbsoluteLayout.addComponent(feuilleIdentiteDeux, "top:220.0px;left:0.0px;");
		//add compute button
		sendButton =  new Button();
		//sendButton.setTabIndex(39);
		sendButton.setCaption(this.messages.getString("send"));
		sendButton.setDescription(this.messages.getString("send"));
		sendButton.setHeight("30px"); 
		sendButton.setWidth("90px"); 
		formAbsoluteLayout.addComponent(sendButton,
				"top:400.0px;left:500.0px;");
		sendButtonListener = new ClickListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				try{
					List<String> namesValuesidentityOne = feuilleIdentiteUn.getNamesValues();
					List<String> firstnamesValuesIdentityOne = feuilleIdentiteUn.getFirstnamesValues();
					List<String> namesValuesidentityTwo = feuilleIdentiteDeux.getNamesValues();
					List<String> firstnamesValuesIdentityTwo = feuilleIdentiteDeux.getFirstnamesValues();
					feuilleIdentiteUn.doNormalizeBirthDate();
					feuilleIdentiteDeux.doNormalizeBirthDate();
					List<Integer> dateOfBirthIditentyOne = feuilleIdentiteUn.getDateOfBirth();
					List<Integer> dateOfBirthIditentyTwo = feuilleIdentiteDeux.getDateOfBirth();
					
					firstPerson.setIdentityValues(firstnamesValuesIdentityOne, namesValuesidentityOne,dateOfBirthIditentyOne);
					secondPerson.setIdentityValues(firstnamesValuesIdentityTwo, namesValuesidentityTwo, dateOfBirthIditentyTwo);
					//Set the language of the people
					firstPerson.setLanguageCode(getCurrentLocale().getLanguage());
					secondPerson.setLanguageCode(getCurrentLocale().getLanguage());
			
					firstPerson.init();
					secondPerson.init();
					
					firstPerson.computeNombreDExpression();
					secondPerson.computeNombreDExpression();
					
					feuilleDeResultatCroise.setFirstPerson(firstPerson);
					feuilleDeResultatCroise.setSecondPerson(secondPerson);
					
					feuilleDeResultatCroise.getInterpret().setFirstPerson(firstPerson);
					feuilleDeResultatCroise.getInterpret().setSecondPerson(secondPerson);
					
					feuilleDeResultatCroise.setResultText();
					
					

				}
				catch(Exception e){
					e.getMessage();
				}
				
			}
		};
		//add feuilleDeResultat
		formAbsoluteLayout.addComponent(feuilleDeResultatCroise, 
				"top:450.0px;left:80.0px;");
		sendButton.addListener(sendButtonListener);
		return formAbsoluteLayout;
	}
	
	public void setCurrentLocaleValue(String currentLocaleValue) {
		this.currentLocaleValue = currentLocaleValue;
	}
	
	public String getCurrentLocaleValue() {
		
		return "_" + currentLocaleValue.toString();
	}
	public Messages getMessages() {
		return this.messages;
	}

	public void setMessages(Messages messages) {
		this.messages = messages;
	}

	public Window getMainWindowCroise(){
		return mainWindowCroise;
	}
}
